package com.ce.pms.charge.dao.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.IDebitTransDao;
import com.ce.pms.charge.model.DebitTransDomain;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.util.SqlConfig;
import com.sitechasia.webx.core.support.Page;

public class DebitTransDaoImpl extends PmsBaseDaoImpl<DebitTransDomain> implements IDebitTransDao {
	/**
	 * 方法描述: 根据原始数据Id查询应收交易实体
	 * @author 胡煦
	 * Create on:2008-8-25
	 * @param chargeDatasId
	 * @return DebitTransDomain
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	//SELECT * FROM FEE_CHARGE_DATAS_R_TRANSACTION cr join FEE_TRANSACTION ft on cr.TRANSACTION_ID = ft.TRANSACTION_ID WHERE ft.CHARGE_DATA_ID = ? and cr.STATUZ = '001' and cr.CLAZZ_CODE = '001'
	public List<DebitTransDomain> getTransDomainBy(Long chargeDatasId){
		 String queryStr =  "select dt from DebitTransDomain dt join dt.chargeDatas dc where dc.id = ? and dt.clazz = '001'";
		 Query query = this.createQuery(queryStr,chargeDatasId);
		 return query.list();
	}

	/**
	 * 方法描述: 查询所有存在收费标准的应收交易
	 * @author 胡煦
	 * Create on:2008-10-10
	 * @param
	 * @return List<DebitTransDomain>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<DebitTransDomain> getTransDomainHasVer(){
		String queryStr = "select dt from DebitTransDomain dt,AbstractStaVersion av where dt.staVersion.id = av.id";
		return createQuery(queryStr).list();
	}
	/**
	 * 功能描述:  给定N多子帐户ID,然后查询指定状态的子帐户交易<br>
	 * @param chargeAccountIds
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-9-24  <br>
	 */
	public List<DebitTransDomain> queryDebitTransDomain(List<Long> chargeAccountIds,String statuz){
		String queryStr = getQueryStr(chargeAccountIds, statuz);
		Query query = super.createQuery(queryStr);
		query.setParameter("statuz", statuz);
		if(chargeAccountIds != null && chargeAccountIds.size() > 0)
			query.setParameterList("ids", chargeAccountIds);
		return query.list();
	}

	/**
	 * 功能描述:  给定N多子帐户ID,然后分页查询指定状态的子帐户交易<br>
	 * @param chargeAccountIds
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-9-24  <br>
	 */
	public Page queryDebitTransDomain(List<Long> chargeAccountIds,String statuz,int currentPage,int pageSize){
		String queryStr = getQueryStr(chargeAccountIds, statuz);
		Map<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("statuz", statuz);
		if(chargeAccountIds != null && chargeAccountIds.size() > 0)
			parameter.put("ids", chargeAccountIds);
		return super.pagedNameQueryByPageNo(queryStr.toString(), currentPage, pageSize, parameter);
	}

	/**
	 * 功能描述:给定N多子帐户ID,然后查询所谓指定状态的子帐户交易的数目<br>
	 * 在银行托收时用到了.在insert交易明细,托收明细时,需要根据具体的数目来确定采用一次更新还是批量更新.
	 * @param chargeAccountIds
	 * @param statuz
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-9-24  <br>
	 */
	public Long getCountForQuery(List<Long> chargeAccountIds,String statuz){
		StringBuffer queryStr = new StringBuffer("SELECT COUNT(debitTran) ");
		queryStr.append(getQueryStr(chargeAccountIds,statuz));
		Query query = super.createQuery(queryStr.toString());
		query.setParameter("statuz", statuz);
		if(chargeAccountIds != null && chargeAccountIds.size() > 0)
			query.setParameterList("ids", chargeAccountIds);
		return (Long)query.uniqueResult();
	}
	/**
	 * 功能描述:  跟新指定子帐户(chargeAccountIds)下面所有的交易状态<br>
	 * @param chargeAccountIds 子帐户ID
	 * @param statuz  需要更新的状态
	 * Create author:yixianjian <br>
	 * Create on:2008-12-2  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void updateDEbitTransDomainToOut(Long bankId){
		SqlConfig sqlConfig = new SqlConfig("BankCollectionSQL.xml");
		String updateStr = sqlConfig.getSql("bankCollectionTransactionUpdate");
		Query query = super.createSQLQuery(updateStr);
		query.setParameter("bankId", bankId);
		query.executeUpdate();
	}
	/**
	 * 功能描述:讲托收明细同子帐户交易建立关系<br>
	 * Create author:yixianjian <br>
	 * Create on:2009-1-12  <br>
	 */
	public void insertDetailRTrans(Long collectionId){
		SqlConfig sqlConfig = new SqlConfig("BankCollectionSQL.xml");
		String updateStr = sqlConfig.getSql("bankCollectionTransactionRefInser");
		Query query = super.createSQLQuery(updateStr);
		query.setParameter("collectionId", collectionId);
		query.executeUpdate();
	}

	private String getQueryStr(List<Long> chargeAccountIds,String statuz){
		StringBuffer queryStr = new StringBuffer("FROM DebitTransDomain debitTran ");
		queryStr.append(" WHERE 1=1 ");
		queryStr.append(" AND debitTran.statuz=:statuz  ");
		if(chargeAccountIds != null && chargeAccountIds.size() > 0){
			queryStr.append(" AND debitTran.bindingAccount.subAccountId IN(:ids)");
		}
		return queryStr.toString();
	}

}
